﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Input;
using WpfFeeCalculator.Controllers;
using WpfFeeCalculator.Locator;
using Microsoft.Practices.Unity;
using WpfFeeCalculator.View;



namespace WpfFeeCalculator.ViewModel
{
    public class MainWindowViewModel : WorkspaceViewModel
    {

        ObservableCollection<WorkspaceViewModel> _workspaces;
        //ReadOnlyCollection<CommandViewModel> _commands;
        RelayCommand _calculateCommand;
        RelayCommand _moreFeesCommand;
        IPortfolioController _portfolioController;

        public MainWindowViewModel()
        {
            RequestClose += MainWindowViewModel_RequestClose;
        }

        public IMainView View { get; set; }

        void MainWindowViewModel_RequestClose(object sender, EventArgs e)
        {
            View.CloseWindow();
        }
        
        public ICommand NewCalculateCommand
        {
            get
            {
                if (_calculateCommand == null)
                    _calculateCommand = new RelayCommand(param => ShowCalculationView());

                return _calculateCommand;
            }
        }



        public ICommand MoreFeesCommand
        {
            get
            {
                if (_moreFeesCommand == null)
                    _moreFeesCommand = new RelayCommand(param => ShowMoreFeesView());

                return _moreFeesCommand;
            }
        }

        public event EventHandler RequestCalculationView;

        void OnRequestCalculationView()
        {
            EventHandler handler = this.RequestCalculationView;
            if (handler != null)
                handler(this, EventArgs.Empty);
        }

        /// <summary>
        /// Returns a read-only list of commands 
        /// that the UI can display and execute.
        /// </summary>
        //public ReadOnlyCollection<CommandViewModel> Commands
        //{
        //    get
        //    {
        //        if (_commands == null)
        //        {
        //            List<CommandViewModel> cmds = this.CreateCommands();
        //            _commands = new ReadOnlyCollection<CommandViewModel>(cmds);
        //        }
        //        return _commands;
        //    }
        //}

        public ObservableCollection<WorkspaceViewModel> Workspaces
        {
            get
            {
                if (_workspaces == null)
                {
                    _workspaces = new ObservableCollection<WorkspaceViewModel>();
                    _workspaces.CollectionChanged += this.OnWorkspacesChanged;
                }
                return _workspaces;
            }
        }

        void OnWorkspacesChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            
        }

        //List<CommandViewModel> CreateCommands()
        //{
        //    return new List<CommandViewModel>
        //    {
        //        new CommandViewModel("MainWindowViewModel_Command_ViewAllCustomers",
        //            new RelayCommand(param => this.ShowCalculationView())),

        //    };
        //}

        void ShowCalculationView()
        {
            var workspace = MainViewModelLocator.Container.Resolve<CalculationViewModel>();
            workspace.RequestClose += workspace_RequestClose;
            this.Workspaces.Add(workspace);
            this.SetActiveWorkspace(workspace);
        }

        void ShowMoreFeesView()
        {
            var workspace = MainViewModelLocator.Container.Resolve<MoreFeesViewModel>();
            workspace.RequestClose += workspace_RequestClose;
            this.Workspaces.Add(workspace);
            this.SetActiveWorkspace(workspace);
        }

        void workspace_RequestClose(object sender, EventArgs e)
        {
            WorkspaceViewModel workspace = sender as WorkspaceViewModel;
            workspace.RequestClose -= workspace_RequestClose;
            workspace.Dispose();
            this.Workspaces.Remove(workspace);
        }

        void SetActiveWorkspace(WorkspaceViewModel workspace)
        {
            Debug.Assert(this.Workspaces.Contains(workspace));

            ICollectionView collectionView = CollectionViewSource.GetDefaultView(this.Workspaces);
            if (collectionView != null)
                collectionView.MoveCurrentTo(workspace);
        }
    }
}
